package example_of_use;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
import weka.classifiers.Evaluation;
import weka.classifiers.bayes.NaiveBayes;
import weka.classifiers.lazy.IBk;
import weka.classifiers.trees.Id3;
import weka.classifiers.trees.J48;
import weka.core.Instances;
import weka.core.converters.ArffSaver;
import weka.core.converters.CSVLoader;
import weka.filters.Filter;
import weka.filters.supervised.attribute.AttributeSelection;
import weka.filters.unsupervised.attribute.Discretize;
import weka.filters.unsupervised.attribute.Normalize;
import weka.filters.unsupervised.attribute.ReplaceMissingValues;

/**
 *
 * @author Reinhard
 */
public class WekaExploration {

  public void convertCsvToArff(String input, String output) throws IOException {
    CSVLoader loader = new CSVLoader();
    loader.setSource(new File(input));
    Instances data = loader.getDataSet();

    ArffSaver saver = new ArffSaver();
    saver.setInstances(data);
    saver.setFile(new File(output));
    saver.setDestination(new File(output));
    saver.writeBatch();
  }

  public Instances doUnsupervisedDiscretize(Instances instances, String[] options) throws Exception {
    Discretize disc = new Discretize();
    disc.setOptions(options);
    disc.setInputFormat(instances);
    Instances ret = Filter.useFilter(instances, disc);
    return ret;
  }

  public Instances doUnsupervisedNormalize(Instances instances, String[] options) throws Exception {
    Normalize norm = new Normalize();
    norm.setOptions(options);
    norm.setInputFormat(instances);
    Instances ret = Filter.useFilter(instances, norm);
    return ret;
  }

  public Instances doUnsupervisedReplaceMissingValue(Instances instances, String[] options) throws Exception {
    ReplaceMissingValues rep = new ReplaceMissingValues();
    rep.setOptions(options);
    rep.setInputFormat(instances);
    Instances ret = Filter.useFilter(instances, rep);
    return ret;
  }

  public Instances doSupervisedAttributeSelection(Instances instances, String[] options) throws Exception {
    AttributeSelection as = new AttributeSelection();
    as.setOptions(options);
    as.setInputFormat(instances);
    Instances ret = Filter.useFilter(instances, as);
    return ret;
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    WekaExploration wex = new WekaExploration();
    BufferedReader reader;
    try {
      reader = new BufferedReader(new FileReader("D:\\Kuliah\\Semester 6\\IF3054 - Intelegensia Buatan\\WEKA\\willwait.arff"));
      Instances training = new Instances(reader);
      reader = new BufferedReader(new FileReader("D:\\Kuliah\\Semester 6\\IF3054 - Intelegensia Buatan\\WEKA\\willwait2.arff"));
      Instances test = new Instances(reader);
      training.setClassIndex(training.numAttributes() - 1);
      test.setClassIndex(test.numAttributes() - 1);

      String[] options = new String[1];
      options[0] = "-U";
      J48 tree = new J48();

      //training = wex.doUnsupervisedNormalize(training, options);

      tree.setOptions(options);
      tree.buildClassifier(training);

      System.out.println("J48");
      //Evaluasi biasa
      //eval.evaluateModel(tree, test);
      
      //Evaluasi cross validate
      //eval.crossValidateModel(tree, test, test.numInstances() , new Random(1));
      
      //Percentage Split
      double percent = 66.0;
      Instances inst = new Instances(training);
      inst.randomize(new Random(5));
      int trainSize = (int) Math.round(inst.numInstances() * percent / 100);
      int testSize = inst.numInstances() - trainSize;
      Instances newtrain = new Instances(inst, 0, trainSize);
      Instances newtest = new Instances(inst, trainSize, testSize);
      
      Evaluation eval = new Evaluation(newtrain);
      eval.evaluateModel(tree, newtest);
      System.out.println(eval.toSummaryString());

      IBk ibk = new IBk(3);
      ibk.buildClassifier(training);

      System.out.println("IBK");
      eval.evaluateModel(ibk, test);  
      System.out.println(eval.toSummaryString());

      NaiveBayes nb = new NaiveBayes();
      nb.buildClassifier(training);

      System.out.println("NaiveBayes");
      eval.evaluateModel(nb, test);
      System.out.println(eval.toSummaryString());

      Id3 id3 = new Id3();
      id3.buildClassifier(training);

      System.out.println("ID3");
      eval.evaluateModel(id3, test);
      System.out.println(eval.toSummaryString());

    } catch (Exception ex) {
      ex.printStackTrace();
    }

  }
}
